package com.kaishengit.dao;

import java.util.*;

import com.kaishengit.entity.Order;
import com.kaishengit.util.DBHelp;
import java.sql.*;

public class OrderDao {

	DBHelp<Order> db=new DBHelp<Order>();
	public boolean save(Order order){
		String sql="insert into t_order (id,address,tel,name,createtime,state,userid) values(?,?,?,?,?,?,?)";
		return db.executeSQL(sql, order.getId(),order.getAddress(),order.getTel(),order.getName(),order.getCreatetime(),order.getState(),order.getUserid());
	}
	
	public List<Map<String, Object>> findByUserId(int userid){
		String sql="SELECT id,createtime,state,(SELECT SUM(total) FROM t_orderitem WHERE orderid=t_order.id) AS 'total' FROM t_order WHERE userid=?";
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		try {
			conn=db.getConnection();
			ps=conn.prepareStatement(sql);
			ps.setInt(1, userid);
			rs=ps.executeQuery();
			while(rs.next()){
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("id", rs.getString("id"));
				map.put("createtime", rs.getString("createtime"));
				map.put("state", rs.getString("state"));
				map.put("total",rs.getFloat("total"));
				list.add(map);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(rs, ps, conn);
		}
		return list;
	}
}
